package com.h117121.fashionshop.core.dom;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;

@SuppressWarnings("serial")
@Entity
@Table(name = "Account")
public class Account extends BaseDom {
	public Account() {
		super();
		money = 0;
	}

	@Transient
	private Cart cart;

	@Column(name = "UserName", nullable = false, unique = true)
	private String userName;

	@Column(name = "UserPassword", nullable = false)
	private String userPassword;

	@Transient
	private String confirmPassword;

	@Column(name = "Name", nullable = true)
	private String name;

	@Column(name = "Birthdate", nullable = true)
	private Date birthday;

	@Enumerated(EnumType.STRING)
	@Column(name = "Sex", nullable = true)
	private Sex sex;

	public enum Sex {
		MALE, FEMALE, GAY, LES, UNKNOWN
	}

	@Enumerated(EnumType.STRING)
	@Column(name = "UserType", nullable = false)
	private UserType userType;

	public enum UserType {
		REGISTERED, ADMIN
	}

	@Column(name = "DateRegistered", nullable = false)
	private Date dateRegistered;

	@Enumerated(EnumType.STRING)
	@Column(name = "Status", nullable = false)
	private AccountStatus status;

	public enum AccountStatus {
		ACTIVE, BANNED
	}

	// @OneToMany(mappedBy = "account", fetch = FetchType.LAZY)
	// private Set<Comment> comments;

	@Column(name = "Money", nullable = false)
	private long money;

	@OneToMany(mappedBy = "account", fetch = FetchType.LAZY)
	@Cascade({ org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	private Set<Invoice> invoices;

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the userPassWord
	 */
	public String getUserPassword() {
		return userPassword;
	}

	/**
	 * @param userPassword
	 *            the userPassWord to set
	 */
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	/**
	 * @return the usetType
	 */
	public UserType getUserType() {
		return userType;
	}

	/**
	 * @param userType
	 *            the usetType to set
	 */
	public void setUserType(UserType userType) {
		this.userType = userType;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the sex
	 */
	public Sex getSex() {
		return sex;
	}

	/**
	 * @param sex
	 *            the sex to set
	 */
	public void setSex(Sex sex) {
		this.sex = sex;
	}

	/**
	 * @return the dateRegistered
	 */
	public Date getDateRegistered() {
		return dateRegistered;
	}

	/**
	 * @param dateRegistered
	 *            the dateRegistered to set
	 */
	public void setDateRegistered(Date dateRegistered) {
		this.dateRegistered = dateRegistered;
	}

	/**
	 * @return the birthday
	 */
	public Date getBirthday() {
		return birthday;
	}

	/**
	 * @param birthday
	 *            the birthday to set
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Override
	public String toString() {
		return "*** " + this.name + " - " + this.userName + " - "
				+ this.userPassword + " - " + this.userType + " ***";
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public Cart getCart() {
		if (cart == null) {
			cart = new Cart();
		}

		return cart;
	}

	public void setCart(Cart cart) {
		this.cart = cart;
	}

	public AccountStatus getStatus() {
		return status;
	}

	public void setStatus(AccountStatus status) {
		this.status = status;
	}

	public void updateInfo(Account account) {
		this.birthday = account.birthday;
		this.name = account.name;
		this.sex = account.sex;
		if (account.userPassword != null && !account.userPassword.isEmpty()) {
			this.userPassword = account.userPassword;
		}
	}

	public void update(Account account) {
		this.birthday = account.birthday;
		this.name = account.name;
		this.sex = account.sex;
		this.status = account.status;
		this.userType = account.userType;
		if (account.userPassword != null && !account.userPassword.isEmpty()) {
			this.userPassword = account.userPassword;
		}
	}

	public long getMoney() {
		return money;
	}

	public void setMoney(long money) {
		this.money = money;
	}
}